package world.hezi.callback;

import com.alibaba.fastjson.JSON;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import world.hezi.pojo.Docs;
import redis.clients.jedis.Jedis;
import world.hezi.pojo.JsonRootBean;
import world.hezi.utils.SearchConcernUtil;

import java.io.IOException;
import java.util.List;

public class BeatsaverHttpCallback implements Callback {

    private final List<Contact> contact;
    private final Jedis jedis;

    public BeatsaverHttpCallback(List<Contact> contact, Jedis jedis) {
        this.contact = contact;
        this.jedis = jedis;
    }

    @Override
    public void onFailure(@NotNull Call call, @NotNull IOException e) {
        System.out.println("失败了");
    }

    @Override
    public void onResponse(@NotNull Call call, Response response) throws IOException {
        ResponseBody body = response.body();
        JsonRootBean jsonRootBean = JSON.parseObject(body.bytes(),JsonRootBean.class);
        boolean flag = false;
        if (SearchConcernUtil.BAN.contains(jsonRootBean.getDocs().get(0).getId())){
            return;
        }
        if (!jedis.exists("lastSong")||!jedis.get("lastSong").equals(jsonRootBean.getDocs().get(0).getId())){
            Docs docs = jsonRootBean.getDocs().get(0);
            boolean china = SearchConcernUtil.isChina(docs.getUploader().getName());
            boolean rank = SearchConcernUtil.isRank(docs);
            boolean chinese = SearchConcernUtil.isChinese(docs.getName());
            List<Long> ids = SearchConcernUtil.search(docs.getUploader().getName());
            jedis.set("lastSong",docs.getId());
            if (!china&&ids.size()==0&&!docs.isRanked()&&!rank&&!chinese){
                System.out.println("不符合条件"+"---"+docs.getId()+"---"+docs.getName()+"---"+docs.getUploader());
                return;
            }
            if (jedis.exists(docs.getId())){
                System.out.println("重复发布");
                return;
            }
            jedis.set(docs.getId(), "1");
            String str = docs.getUploader().getName() +
                    "发布了一首新谱！\r\n" +
                    "歌名是：" +
                    docs.getName() +
                    "\r\n链接是：" +
                    "https://beatsaver.com/maps/" +
                    docs.getId();
            MessageChainBuilder messageChainBuilder = new MessageChainBuilder();
            if (china){
                messageChainBuilder.append("【OK兄弟们，全体目光向我看齐，我宣布个事】\r\n国区谱师");
                flag = true;
            }

            if (rank){
                messageChainBuilder.append("新Rank谱!\r\n");
                flag = true;
            }

            if (chinese){
                messageChainBuilder.append("中文谱！(可能不是)\r\n");
                flag = true;
            }
            messageChainBuilder.append(str);
//            contact.sendMessage(chain);
            MessageChain chain = messageChainBuilder.build();
            if(flag){
                for (Contact contact1 : contact) {
                    if (contact1.getId()!=941873202L)
                        contact1.sendMessage(chain);
                }
            }
            for (Long id : ids) {
                messageChainBuilder.append(" ").append(new At(id));
            }
            MessageChain chainDF = messageChainBuilder.build();
            for (Contact contact1 : contact) {
                if (contact1.getId()==941873202L)
                    contact1.sendMessage(chainDF);
            }
            System.out.println("发送新歌");
        }else {
            System.out.println("发过了");
        }
    }
}
